﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.DnsToolkit.Packet;
using System.IO;
using System.Data.Linq;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.Serializer;
using System.Net.DnsToolkit.Serializer.Stream;
using System.Net.DnsToolkit.Serializer.Stream.Reader;
using System.Net.DnsToolkit.Configuration;
using Ninject;

namespace System.Net.DnsToolkit.Scanner.Helper {

    public static class HostHelper {

        static readonly IMessageReader<Stream> _messageReader;
        static HostHelper() {

            var dsr = new DnsReaderStreamModule();
            IKernel kernel = new StandardKernel(dsr);
            _messageReader = kernel.Get<IMessageReader<System.IO.Stream>>();
        }

        public static IMessage GetMessage(this Host host) {

            if (host == null) {
                return null;
            }

            if (host.Data == null) {
                return null;
            }
            Binary bin = host.Data;
            int offset = 0;
            MemoryStream mem = new MemoryStream(bin.ToArray(), offset, bin.Length);
            IMessage message = _messageReader.Read(mem);
            return message;

        }

    }
}
